package service.impl;


import bean.CutPageBean;
import bean.StudentBean;
import mapper.IStudentMapper;
import service.IStudentService;
import org.apache.ibatis.session.SqlSession;

public class StudentServiceImpl extends BaseService implements IStudentService {
    @Override
    public StudentBean getById(Integer id) {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        StudentBean studentBean = mapper.getById(id);
        session.close();
        return studentBean;
    }

    @Override
    public void insert(StudentBean studentBean) {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        mapper.insert(studentBean);
        session.commit();
        session.close();
    }

    @Override
    public void delete(Integer id) {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        mapper.delete(id);
        session.commit();
        session.close();
    }

    @Override
    public void update(Integer id, Integer roomId) {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        mapper.update(id, roomId);
        session.commit();
        session.close();
    }

    @Override
    public CutPageBean<StudentBean> getStudentByPage(int current, String name, String className, String roomAddr) {
        CutPageBean<StudentBean> cutPageBean = new CutPageBean<>();
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        cutPageBean.setList(mapper.getStudentByPage((current - 1) * CutPageBean.PAGESIZE, CutPageBean.PAGESIZE, name, className, roomAddr));
        cutPageBean.setCount(mapper.CutPageCount(name, className, roomAddr));
        session.close();
        return cutPageBean;
    }
}
